package com.lagou.module5_5;

import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author wsl
 * @Date 2021/8/10 21:39
 * @Project_Name javase
 */
public class ListTest {

    public static void main(String[] args) {
        /**
         * 二、需求描述：电商--->视频订单数据处理：两个人分别有订单List<VideoOrder> list1和list2。完成如下功能：
         */

        //1.创建List集合list1和list2
        List<VideoOrder> list1 = Arrays.asList(
            new VideoOrder("20194350812",  5,"微服务SpringCloud"),
            new VideoOrder("20190814232", 9, "Redis教程"),
            new VideoOrder("20190523812", 9 ,  "⽹⻚开发教程"),
            new VideoOrder("201932324", 9 , "百万并发实战Netty"));

        List<VideoOrder> list2 = Arrays.asList(
            new VideoOrder("20190814232", 9, "Redis教程"),
            new VideoOrder("20190522338312", 9 , "⽹⻚开发教程"),
            new VideoOrder("2019435230812", 5 , "Jmeter压⼒测试"),
            new VideoOrder("2019323542411", 7 , "Git+Jenkins持续集成"),
            new VideoOrder("2019323542424", 21 , "Idea全套教程"));

       /* boolean contains = true;
        for (VideoOrder order: list1){
            for (VideoOrder order1: list2){
                contains = order.getTitle().contains(order1.getTitle());
                //System.out.println("contains= "+contains);
            }
        }
        System.out.println("contains= "+contains);*/
        //2.1、统计出同时被两个人购买的商品列表（取交集）
        //List<VideoOrder> orderList01 = list1.stream().filter(obj -> list2.contains(obj)).collect(Collectors.toList());
        List<VideoOrder> orderList01 = list1.stream().filter(list2::contains).collect(Collectors.toList());
        System.out.println(orderList01);

        System.out.println("--------------------------------------------------------------------");
        //2.2、统计出两个人购买商品的差集
        // 订单一和订单二 差集
        List<VideoOrder> orderList02 = list1.stream().filter(obj -> !list2.contains(obj)).collect(Collectors.toList());
        System.out.println(orderList02);

        // 订单二和订单一 差集
        List<VideoOrder> orderList03 = list2.stream().filter(obj -> !list1.contains(obj)).collect(Collectors.toList());
        System.out.println(orderList03);

        System.out.println("--------------------------------------------------------------------");
        //2.3、统计出全部被购买商品的去重并集
        //拿到并集 list
        List<VideoOrder> list = list1.stream().parallel().collect(Collectors.toList());
        list.addAll(list2);
        //System.out.println("list= "+list);
        //去重复
        List<VideoOrder> orderList04 = list.stream().distinct().collect(Collectors.toList());
        System.out.println(orderList04);

        System.out.println("--------------------------------------------------------------------");
        //2.4、统计两个人的分别购买订单的平均价格
        // 统计订单一的平均价格
        //Double avg1 = list1.stream().collect(Collectors.averagingDouble(obj -> obj.getMoney()));
        Double avg1 = list1.stream().collect(Collectors.averagingDouble(VideoOrder::getMoney));
        System.out.println("avg1= "+avg1);//8.0

        // 统计订单二的平均价格
        //Double avg2 = list2.stream().collect(Collectors.averagingDouble(obj -> obj.getMoney()));
        Double avg2 = list2.stream().collect(Collectors.averagingDouble(VideoOrder::getMoney));
        System.out.println("avg2= "+avg2);//10.2

        System.out.println("--------------------------------------------------------------------");
        //2.5、统计两个人的分别购买订单的总价格
        // 统计订单一的总价格
        //int sum1 = list1.stream().collect(Collectors.summingInt(obj -> obj.getMoney())).intValue();
        int sum1 = list1.stream().mapToInt(VideoOrder::getMoney).sum();
        System.out.println("sum1= "+sum1);//32

        // 统计订单二的总价格
        //int sum2 = list2.stream().collect(Collectors.summingInt(obj -> obj.getMoney())).intValue();
        int sum2 = list2.stream().mapToInt(VideoOrder::getMoney).sum();
        System.out.println("sum2= "+sum2);//51
    }
}
